package com.longxi.tianjintianbao.ui.activity.assetscheck;

import android.content.Intent;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.controller.DataInventoryController;
import com.longxi.tianjintianbao.greedao.entity.Inventory;
import com.longxi.tianjintianbao.greedao.entity.InventoryChild;
import com.longxi.tianjintianbao.ui.adapter.multitype.inventory.InventoryChildViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.inventory.InventoryViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by cxw on & 2018/12/13
 */
public class AssetsWebServiceInventoryChildActivity extends BaseActivity {

    @BindView(R.id.tabLayout)
    SegmentTabLayout mTabLayout;
    private String[] title = {"全部", "未盘", "已盘", "盘盈"};

    private MultiTypeAdapter adapter;
    private Items items = new Items();
    int page = 0;
    int limit = 10;
    Inventory inventory;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_assets_web_service_inventory_child;
    }

    @Override
    public void initView() {
        super.initView();
        inventory = DataInventoryController.getPatrolByBillid(getIntent().getStringExtra(AppConst.BILLID));
        mTabLayout.setTabData(title);
        initAdapter();
    }

    private void initAdapter() {
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            adapter.register(InventoryChild.class, new InventoryChildViewBinder(new InventoryChildViewBinder.OnClickListener() {
                @Override
                public void onClick(int position) {
                }
            }));
            mRvContent.setAdapter(adapter);
            adapter.setItems(items);
        }
    }

    @Override
    public void initListener() {
        super.initListener();
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                getInventoryChildList();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page=0;
                getInventoryChildList();
            }
        });
        mRefreshLayout.autoRefresh();
    }

    private void getInventoryChildList(){
        List<InventoryChild> list = DataInventoryController.SearchInventoryChild(inventory, page, limit, 0);
        if(page == 0){
            items.clear();
        }
        items.addAll(list);
        adapter.notifyDataSetChanged();
        if(list.size() < limit){
            MyUtils.showToast("没有更多数据了");
        }
        if(items.size() > 0){
            hideLoading();
        }else{
            showNoData();
        }
        if(mRefreshLayout.isRefreshing()){
            mRefreshLayout.finishRefresh();
        }

        if(mRefreshLayout.isLoading()){
            mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
